package com.example.campusforumapi.controller;

import com.example.campusforumapi.entity.Department;
import com.example.campusforumapi.entity.User;
import com.example.campusforumapi.service.UserService;
import com.example.campusforumapi.util.AliOSSUtils;
import com.example.campusforumapi.util.PageQueryUtil;
import com.example.campusforumapi.util.PageResult;
import com.example.campusforumapi.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Primary
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    //    查询所有记录
    @GetMapping("/queryAll")
    public Result queryAll() {
        List<User> userList = userService.queryAll();
        return Result.success(userList);
    }
    //    新增一条记录
    @PostMapping ("/insert")
    public Result insert(@RequestBody User user){
        if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getDepartmentId())){
            return Result.error("用户名及院系id不能为空");
        }
        else if (userService.findsameUsername(user.getUsername())>=1) {
            return Result.error("用户名已存在,请更换新的用户名！");
        }
        else if(userService.findsameUserId(user.getUserId())>=1){
            return Result.error("该学号已存在");
        }else if(user.getPhone().length()!=11){
            return Result.error("手机号格式异常,请输入11位手机号！");
        }else if(user.getBirthdate()==""){
            return Result.error("出生日期不能为空");
        }
        else if(userService.insertUser(user) > 0){
            return Result.success("插入成功");
        }else{
            return Result.error("插入异常");
        }

    }

    //    修改一条记录
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        if(user.getBirthdate()==""){
            return Result.error("出生日期不能为空");
        }else if(user.getPhone()!=""&&user.getPhone()!=null&&user.getPhone().length()!=11){
            return Result.error("手机号格式异常,请输入11位手机号！");
        }else if(userService.updUser(user) > 0){
            return Result.success("修改成功");
        }
        else{
            return Result.error("修改失败，该用户不存在");
        }

    }

    //    删除一条记录
    @DeleteMapping ("/delete/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        userService.delUser(ids);
        return Result.success();
    }

    /*
     * 按条件  分页查询
     * */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize
    , Integer userId,String username, Integer departmentId,String gender){

        //        查询并封装分页结果集
        PageResult userPage = userService.getUserPage(page,pageSize,userId,username,departmentId,gender);

        return Result.success(userPage);
    }

    /*
    * 图片文件上传 获取阿里云url地址
    * */
    @PostMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile avatar) throws IOException{
        log.info("文件上传，文件名：{ }",avatar.getOriginalFilename());
        String url = aliOSSUtils.upload(avatar);
        return Result.success(url);

    }

    /*
     * 修改编辑数据回显
     * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }


    //    查询所有学院信息记录
    @GetMapping("/getDepartments")
    public Result getDepartments() {
        List<Department> departmentList = userService.getDepartments();
        return Result.success(departmentList);
    }
    //    查询加密密码是否存在记录
    @GetMapping("/getPassword")
    public Result getPassword(User user) {
        int passwordcount = userService.getPassword(user);
        return Result.success(passwordcount);
    }
}
